package com.useeinfo.asg.open.web;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.ProjectBiz;
import com.useeinfo.asg.modules.logging.biz.VersionBiz;
import com.useeinfo.asg.modules.logging.entity.Project;
import com.useeinfo.asg.modules.logging.entity.Version;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 官网定义
 *
 * Created by 宝玉丶 on 2017/8/28.
 */
@Controller
@RequestMapping("/web")
public class IndexWebAction {

    private static Logger logger = LoggerFactory.getLogger(IndexWebAction.class);

    @Autowired
    private ProjectBiz projectBiz;
    @Autowired
    private VersionBiz versionBiz;

    /**
     * 版本管理
     * @param request
     * @return
     */
    @RequestMapping("/version_{projectId}")
    public String index(HttpServletRequest request, ModelMap mv, @PathVariable String projectId) {

        long projId = CommUtil.null2Long(projectId);
        Project project = projectBiz.findModel(projId);
        if(project != null) {
            QueryParam queryParam = new QueryParam(-1, -1);
            queryParam.getSqlMap().put("projId", CommUtil.null2String(project.getId()));
            List<Version> versionList = versionBiz.findList(queryParam);
            mv.put("project", project);
            mv.put("versionList", versionList);
        }
        return "/pages/view/version";
    }

    @RequestMapping("/project")
    public String weblist(HttpServletRequest request, ModelMap mv) {

        return "/pages/view/project";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(HttpServletRequest request,
                               @RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "projectName", required = false) String projectNameParam,
                               @RequestParam(value = "proManager", required = false) String proManagerParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        if (!CommUtil.null2String(projectNameParam).equals("")) {
            queryParam.getSqlMap().put("projectName", projectNameParam);
        }
        if (!CommUtil.null2String(proManagerParam).equals("")) {
            queryParam.getSqlMap().put("proManager", proManagerParam);
        }

        return projectBiz.findJSONList(queryParam);
    }
}
